//Basic Ajax Routine- Author: Dynamic Drive (http://www.dynamicdrive.com)
//Last updated: Jan 15th,06'
//rework by Denis on 20060922 : added 'target' param/property
//rework by Denis on 20061010 : added p0..p9 params,added multi-request functionality

function createAjaxRequest(){
var httprequest=false;
if (window.XMLHttpRequest){ // if Mozilla,Safari etc
httprequest=new XMLHttpRequest()
//if (httprequest.overrideMimeType)
//httprequest.overrideMimeType('text/xml')
}
else if (window.ActiveXObject){ // if IE
try {httprequest=new ActiveXObject("Msxml2.XMLHTTP");}
catch (e){try{httprequest=new ActiveXObject("Microsoft.XMLHTTP");}catch (e){}}}
return httprequest;
}

var ajaxpack=new Object();
ajaxpack.ajaxobj = false;
ajaxpack.target = false;
ajaxpack.basedomain="http://"+window.location.hostname
ajaxpack.filetype="txt"
ajaxpack.addrandomnumber=1 //Set to 1 or 0. See documentation.

ajaxpack.getAjaxRequest=function(url,parameters,callbackfunct,filetype,target,p0,p1,p2,p3,p4,p5,p6,p7,p8,p9){
var ajaxobj=createAjaxRequest();
if (ajaxobj){
this.ajaxobj = ajaxobj;
this.target = target;
this.filetype = filetype;
if (ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
ajaxobj.onreadystatechange=function(){callbackfunct(ajaxobj,target,filetype,p0,p1,p2,p3,p4,p5,p6,p7,p8,p9)};
ajaxobj.open('GET',url+"?"+parameters,true);
ajaxobj.send(null);
}
}

ajaxpack.postAjaxRequest=function(url,parameters,callbackfunct,filetype,target,p0,p1,p2,p3,p4,p5,p6,p7,p8,p9){
var ajaxobj=createAjaxRequest();
if (ajaxobj){
this.ajaxobj = ajaxobj;
this.target = target;
this.filetype = filetype;
if (ajaxpack.addrandomnumber==1) //Further defeat caching problem in IE?
var parameters=parameters+"&ajaxcachebust="+new Date().getTime()
ajaxobj.onreadystatechange=function(){callbackfunct(ajaxobj,target,filetype,p0,p1,p2,p3,p4,p5,p6,p7,p8,p9)};
ajaxobj.open('POST',url,true);
ajaxobj.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajaxobj.setRequestHeader("Content-length",parameters.length);
ajaxobj.setRequestHeader("Connection","close");
ajaxobj.send(parameters);
}
}


//ACCESSIBLE VARIABLES (for use within your callback functions):
//1) ajaxpack.ajaxobj //points to the current ajax object
//2) ajaxpack.filetype //The expected file type of the external file ("txt" or "xml")
//3) ajaxpack.ajaxobj.basedomain //The root domain executing this ajax script,taking into account the possible "www" prefix.
//5) ajaxpack.target //target holder for callbackfunc or somethig else...

//ACCESSIBLE FUNCTIONS:
//1) ajaxpack.getAjaxRequest(url,parameters,callbackfunc,filetype,target,p0,p1,p2,p3,p4,p5,p6,p7,p8,p9)
//2) ajaxpack.postAjaxRequest(url,parameters,callbackfunc,filetype,target,p0,p1,p2,p3,p4,p5,p6,p7,p8,p9)

///////////END OF ROUTINE HERE////////////////////////


//////EXAMPLE USAGE ////////////////////////////////////////////
/* Comment begins here

//Define call back function to process returned data
    function processAjaxGet(sender,target,filetype,p0,p1,p2,p3,p4,p5,p6,p7,p8,p9){
        var myreq = sender.ajaxobj;
        if (myreq.readyState == 4){ //if request of file completed
            if (myreq.status==200 || window.location.href.indexOf("http://")!=0){ //if request was successful or running script locally
                if (filetype=="txt"){
                    if (target) {
                        target.innerHTML = myreq.responseText;
                    }
                    else
                        alert('ERROR: no target');
                }
            }
        }
    }

/////1) GET Example- alert contents of any file (regular text or xml file):

ajaxpack.getAjaxRequest("example.php","",processGetPost,"txt")
ajaxpack.getAjaxRequest("example.php","name=George&age=27",processGetPost,"txt")
ajaxpack.getAjaxRequest("examplexml.php","name=George&age=27",processGetPost,"xml")
ajaxpack.getAjaxRequest(ajaxpack.basedomain+"/mydir/mylist.txt","",processGetPost,"txt")

/////2) Post Example- Post some data to a PHP script for processing,then alert posted data:

//Define function to construct the desired parameters and their values to post via Ajax
function getPostParameters(){
var namevalue=document.getElementById("namediv").innerHTML //get name value from a DIV
var agevalue=document.getElementById("myform").agefield.value //get age value from a form field
var poststr = "name=" + encodeURI(namevalue) + "&age=" + encodeURI(agevalue)
return poststr
}

var poststr=getPostParameters()

ajaxpack.postAjaxRequest("example.php",poststr,processGetPost,"txt")
ajaxpack.postAjaxRequest("examplexml.php",poststr,processGetPost,"xml")

Comment Ends here */


